package artid.com.arttogo.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import artid.com.arttogo.R;
import artid.com.arttogo.bean.LookRemindParser;
import artid.com.arttogo.bean.OneStateIntParser;
import artid.com.arttogo.bean.SpecialInfoParser;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.MyBitmapImageViewTarget;
import artid.com.arttogo.utils.NetUtils;
import artid.com.arttogo.utils.ToastUtil;
import artid.com.arttogo.view.MyEditText;
import artid.com.arttogo.view.remindertime.ReminderTime_Utils;

/**
 * 
 *    
 * 项目名称：ArtworkArt   
 * 类名称：AuctionAddRemind_Activity   
 * 类描述：添加提醒   
 * 创建人：耿卫斌   
 * 创建时间：2017-3-2 下午3:49:31   
 *
 */
public class AuctionAddRemind_Activity extends BaseActivity implements OnClickListener {
	private ImageView img;
	private TextView mTitleRight,state,title,previewTime,previewAddress,company,address,remindTime;
	private RelativeLayout remindTimeRL;
	private MyEditText memoEditText;
	private ReminderTime_Utils mPopReminderTime;
	private SpecialInfoParser mSpecialInfoParser;//传递过来的专场信息
	private String Type="1";//类型	type	1.添加  2.修改 3.删除(默认添加)
	private int is_remind;//0 已经开拍，不能点击1.添加，2. 查看提醒
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_auctionaddremind);
		initView();
	}

	private void initView(){
		mContext=AuctionAddRemind_Activity.this;
		mSpecialInfoParser=(SpecialInfoParser) getIntent().getSerializableExtra("SpecialInfoParser");
		mBack = (ImageView) findViewById(R.id.back_btn);
		mBack.setOnClickListener(this);
		mTittle = (TextView) findViewById(R.id.tittle_text);
		mTittle.setText("添加提醒");
		mTitleRight = (TextView) findViewById(R.id.tittle_text_info);
		mTitleRight.setText("完成");
		mTitleRight.setEnabled(false);
		mTitleRight.setTextColor(Color.parseColor("#CDCDCD"));
		mTitleRight.setVisibility(View.VISIBLE);
		mTitleRight.setOnClickListener(this);
		img=(ImageView)  findViewById(R.id.AuctionAddRemind_img);
		state=(TextView)  findViewById(R.id.AuctionAddRemind_state);
		title=(TextView)  findViewById(R.id.AuctionAddRemind_title);
		previewTime=(TextView)  findViewById(R.id.AuctionAddRemind_previewTime);
		previewAddress=(TextView)  findViewById(R.id.AuctionAddRemind_previewAddress);
		company=(TextView)  findViewById(R.id.AuctionAddRemind_company);
		address=(TextView)  findViewById(R.id.AuctionAddRemind_address);
		remindTimeRL=(RelativeLayout)  findViewById(R.id.AuctionAddRemind_remindTimeRL);
		remindTimeRL.setOnClickListener(this);
		remindTime=(TextView)  findViewById(R.id.AuctionAddRemind_remindTime);
		memoEditText=(MyEditText)  findViewById(R.id.AuctionAddRemind_memoEditText);
		memoEditText.addTextChangedListener(mTextChangedListener);
		mPopReminderTime=new ReminderTime_Utils(mContext);
		mPopReminderTime.setOnClickOkListener(onTimeClick);
		setViewData();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_btn:
			finish();
			break;
		case R.id.tittle_text_info:
			//完成按钮
			try {
				postAddRemind();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.d("log", "时间转换错误");
			}
			break;
		case R.id.AuctionAddRemind_remindTimeRL:
			//添加提醒时间
			mPopReminderTime.show(v);
			conditionJudge();
			break;

		default:
			break;
		}
	}

	/**
	 * 给View赋值
	 */
	private void setViewData(){
		Glide.with(mContext).load(mSpecialInfoParser.getImgurl()
				+ "_"+ (Integer.parseInt(NetUtils.getDisplayMetrics((Activity)mContext).get(NetUtils.DM_WIDTH)) /2) + "x10.png"
				).asBitmap().centerCrop()
				.placeholder(R.drawable.ic_menu_rotate).error(R.drawable.ic_menu_rotate)
				.into(new MyBitmapImageViewTarget(img));
		title.setText(mSpecialInfoParser.getTitle());
		int status=mSpecialInfoParser.getStatus();
		switch (status) {
		case 0:
			state.setText("已结束");
			previewTime.setText("拍卖时间："+mSpecialInfoParser.getStart_time()+"-"+mSpecialInfoParser.getEnd_time());
			previewAddress.setText("拍卖地点："+mSpecialInfoParser.getAuction_address());
			break;
		case 1:
			state.setText("待预展");
			previewTime.setText("预展时间："+mSpecialInfoParser.getStart_time()+"-"+mSpecialInfoParser.getEnd_time());
			previewAddress.setText("预展地点："+mSpecialInfoParser.getPreview_address());
			break;
		case 2:
			state.setText("预展中");
			previewTime.setText("预展时间："+mSpecialInfoParser.getStart_time()+"-"+mSpecialInfoParser.getEnd_time());
			previewAddress.setText("预展地点："+mSpecialInfoParser.getPreview_address());
			break;
		case 3:
			state.setText("拍卖中");
			previewTime.setText("拍卖时间："+mSpecialInfoParser.getStart_time()+"-"+mSpecialInfoParser.getEnd_time());
			previewAddress.setText("拍卖地点："+mSpecialInfoParser.getAuction_address());
			break;

		default:
			break;
		}
		company.setText(mSpecialInfoParser.getSeller_name());
		address.setText(mSpecialInfoParser.getAddress());
		is_remind=mSpecialInfoParser.getIs_remind();
		switch (is_remind) {
		case 1:
			//1.添加
			Type="1";
			break;
		case 2:
			//2. 查看提醒
			Type="2";
			getRemind();
			break;
		default:
			break;
		}
	}

	/**
	 * 提醒时间选择
	 */
	ReminderTime_Utils.OnClickOkListener onTimeClick=new ReminderTime_Utils.OnClickOkListener() {

		@Override
		public void onClickOk(String year, String month, String day, String hour) {
			// TODO Auto-generated method stub
			remindTime.setText(year+"年"+month+"月"+day.substring(0,2)+"日"+hour);
			conditionJudge();
		}
	};

	/**
	 * 条件判断
	 */
	private void conditionJudge() {
		// 对按钮的判断是否可用
		if (!TextUtils.isEmpty(remindTime.getText().toString())&& !TextUtils.isEmpty(memoEditText.getText().toString())) {
			mTitleRight.setEnabled(true);
			mTitleRight.setTextColor(Color.parseColor("#000000"));
		} else {
			mTitleRight.setEnabled(false);
			mTitleRight.setTextColor(Color.parseColor("#CDCDCD"));
		}
	}


	/**
	 * 对EditText监听
	 */
	TextWatcher mTextChangedListener = new TextWatcher() {
		private CharSequence temp;

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
								  int count) {
			// TODO Auto-generated method stub
			temp = s;
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
									  int after) {
			// TODO Auto-generated method stub
			// mTextView.setText(s);//将输入的内容实时显示
		}

		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub
			conditionJudge();
		}
	};

	// 把字符串转为日期
	public String dataOne(String time) {
		SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH时",  Locale.CHINA);
		Date date;
		String times = null;
		try {  
			date = sdr.parse(time);  
			long l = date.getTime();  
			String stf = String.valueOf(l);
			times = stf.substring(0, 10);  
		} catch (Exception e) {
			e.printStackTrace();  
		}  
		return times;  
	}  

	// 将时间戳转为字符串
	public String getStrTime(String cc_time) {
		String re_StrTime = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时");
		// 例如：cc_time=1291778220
		long lcc_time = Long.valueOf(cc_time);
		re_StrTime = sdf.format(new Date(lcc_time*1000));
		return re_StrTime;
	}

	/**
	 * 提交添加提醒
		专场id	id	
		提醒时间	time	需要为时间戳
		备忘录	content	可以为空
		类型	type	1.添加  2.修改 3.删除
	 * @throws Exception
	 */
	private void postAddRemind() throws Exception {
		VolleyBasic.getInstance(this).post(Cache.specialRemind,
				VolleyMap.getInstance().specialRemind(mSpecialInfoParser.getId(), dataOne(remindTime.getText().toString())+"",
						memoEditText.getText().toString(), Type), 
						OneStateIntParser.class, new VolleyCallback() {

			@SuppressWarnings({ "unchecked", "static-access" })
			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				OneStateIntParser bean=(OneStateIntParser) vd.getData();
				if(bean.getState()==1){
					//通知专场详情提醒状态改变
					Intent intent = new Intent("com.AuctionSpecialInfo_Activity.receiver");
					intent.putExtra("addRemind", true);
					intent.putExtra("specialId", mSpecialInfoParser.getId());
					mContext.sendBroadcast(intent);
					//通知专场列表提醒状态改变
					Intent intent2 = new Intent("com.AuctionSpecial_Activity.receiver");
					intent2.putExtra("addRemind", true);
					intent2.putExtra("specialId", mSpecialInfoParser.getId());
					mContext.sendBroadcast(intent2);
					//通知拍卖会详情里专场列表提醒状态改变
					Intent intent3 = new Intent("com.AuctionInfo_Activity.receiver");
					intent3.putExtra("addRemind", true);
					intent3.putExtra("specialId", mSpecialInfoParser.getId());
					mContext.sendBroadcast(intent3);
					ToastUtil.toast(mContext, "添加成功");
					finish();
				}else{
					ToastUtil.toast(mContext, "提交失败");
				}
			}

			@Override
			public void onError(VolleyError volleyError) {
				// TODO Auto-generated method stub
				ToastUtil.toast(mContext, "提交失败");
			}
		});
	}

	/**
	 * 查看提醒
	 * 专场id	id
	 */
	private void getRemind(){
		VolleyBasic.getInstance(this).post(Cache.lookRemind, 
				VolleyMap.getInstance().lookRemind(mSpecialInfoParser.getId()), 
				LookRemindParser.class, new VolleyCallback() {

			@SuppressWarnings({ "unchecked", "static-access" })
			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				LookRemindParser bean=(LookRemindParser) vd.getData();
				mSpecialInfoParser.setId(bean.getSpecial_id());
				remindTime.setText(getStrTime(bean.getRemind_time()));
				memoEditText.setText(bean.getContent());
			}

			@Override
			public void onError(VolleyError volleyError) {
				// TODO Auto-generated method stub
			}
		});
	}
}
